﻿using System;
using System.Linq.Expressions;
using System.Web.UI;
using SBACN.WebSite.Code;
using SBACN.WebSite.Common;
using SBACN.WebSite.Models;

namespace SBACN.WebSite
{
    public partial class Complain : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string vid = Request.QueryString["vid"];
                if (!string.IsNullOrEmpty(vid))
                {
                    ValidateUser(vid);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblValid.Visible = false;
            if (this.txt_valid.Text.Trim().ToLower() != Session["Complain"].ToString().ToLower())
            {
                lblValid.Visible = true;
                return;
            }

            Expression<Func<TB_PublicUser, bool>> ff = (p => p.EMail == txt_Email.Text.Trim());
            var resultR = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_PublicUser>(ff);
            bool isValid = false;//是否验证过
            int status = -1;//投诉信息是否有效
            string vid = string.Empty;
            string email = string.Empty;
            string username = string.Empty;
            if (resultR != null)
            {
                email = resultR.EMail;
                username = resultR.Name;
                //存在这个邮箱
                resultR.Name = txt_Name.Text.Trim();
                resultR.Telephone = txt_Tel_1.Text.Trim() + txt_Tel_2.Text.Trim() + txt_Tel_3.Text.Trim();
                resultR.CreateDate = DateTime.Now;
                DAL.DALHelper.Instance.Update<TB_PublicUser>(resultR);
                DAL.DALHelper.Instance.SaveChanges();

                isValid = resultR.Satus;
                if (isValid)
                    status = 0;
                vid = resultR.ValidCode;
            }
            else
            {
                //添加User信息
                TB_PublicUser user = new TB_PublicUser();
                email = user.EMail = txt_Email.Text.Trim();
                username = user.Name = txt_Name.Text.Trim();
                user.Telephone = txt_Tel_1.Text.Trim() + txt_Tel_2.Text.Trim() + txt_Tel_3.Text.Trim();
                user.CreateDate = DateTime.Now;
                user.Satus = false;
                vid = user.ValidCode = Guid.NewGuid().ToString();
                DAL.DALHelper.Instance.AddWithTransaction<TB_PublicUser>(user);

            }

            //添加投诉信息
            TB_Feedback f = new TB_Feedback();
            f.UserIdentity = txt_Email.Text.Trim();
            f.Title = txt_title.Text.Trim();
            f.Content = txt_Complain.Text.Trim();
            f.Status = status;
            f.CreateDate = DateTime.Now;
            f.Type = "00090003";
			f.Memo = string.Empty;
            DAL.DALHelper.Instance.AddWithTransaction<TB_Feedback>(f);

            //发邮件 
            if (!string.IsNullOrEmpty(email))
            {
                string code = "2";//默认未验证用户
                if (isValid)
                    code = "3"; //已验证过了
                Expression<Func<TB_SystemConfig, bool>> fff = (p => p.Type == "00090007" & p.Code == code);
                var resultSystemConfig = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_SystemConfig>(fff);
                if (resultSystemConfig != null && resultSystemConfig.Status)
                {
					//if (code == "2")
					//{
						string content = resultSystemConfig.Value;
						string url = "http://" + Request.Url.Host + "/Complain.aspx?vid=" + vid;
						content = content.Replace("$URL", url);
						//content = content.Replace("$Name", resultR.Name);
						content = content.Replace("$Name", username);
						Mail.Instance.SendEMail(resultSystemConfig.Name.Replace("$Name", username), content, email);
					//}
                }
                //显示字符
                if (isValid)
                {
                    lblShow.Text = "您的投诉已成功提交，我们会及时进行处理!";
                }
                else
                {
					lblShow.Text = "为确保您的投诉能被有效受理，系统已发送验证邮件到您的邮箱：<font color=red><b>" + txt_Email.Text.Trim() + "</b></font>，请您登陆邮箱激活验证邮件，激活后您的投诉信息会即可反映至上海市银行同业公会!";
                }
            }
            else
            {
                lblShow.Text = "您没有邮箱信息 !";
            }
            Page.ClientScript.RegisterStartupScript(GetType(), "show", "<script>ShowMessage();</script>");
        }

        private void ValidateUser(string vid)
        {
            Expression<Func<TB_PublicUser, bool>> ff = (p => p.ValidCode == vid);
            var resultR = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_PublicUser>(ff);
            if (resultR != null)
            {
                resultR.ValidDate = DateTime.Now;
                resultR.Satus = true;
                DAL.DALHelper.Instance.Update<TB_PublicUser>(resultR);

                Expression<Func<TB_Feedback, bool>> fff = (p => p.UserIdentity == resultR.EMail);
                var resultFeedBack = DAL.DALHelper.Instance.GetList<TB_Feedback>(fff);

                foreach (TB_Feedback fb in resultFeedBack)
                {
                    fb.Status = 1;
                    DAL.DALHelper.Instance.Update<TB_Feedback>(fb);
                }
                DAL.DALHelper.Instance.SaveChanges();
                lblShow.Text = "验证已通过，您填写的投诉已成功提交，我们会尽快进行处理!";
                Page.ClientScript.RegisterStartupScript(GetType(), "show", "<script>ShowMessage();</script>");
            }
        }

    }
}